//Copyright 2009 Dominik Punz
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

// Desktop (instantiate JTF COM Wrapper)
var wshShell = new ActiveXObject("WScript.Shell");
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var desktop = new ActiveXObject("SilkTestLib.Desktop");
var cwd = desktop;
var defaultEditor = "notepad.exe";
//desktop.Connect("localhost", 11199); info service dynamically choses any port, so there is no longer a default port...

function printLine(text) {
    WScript.StdOut.WriteLine(text);
};

function print(text) {
    WScript.StdOut.Write(text);
};

function conditionalPrintLine(text, pattern) {
    if (pattern.length == 0 || text.search(pattern) != -1)
        printLine(text);
};

function readLine() {
    return WScript.StdIn.ReadLine()
};

function getClassName(testObject) {
    var classNameArray = testObject.getClass().split(".");
    var className = classNameArray[classNameArray.length - 1];
    return className;
};

function getSimpleLocator(testObject) {
    var locator = getClassName(testObject);		
    var caption = testObject.getProperty("caption");
    if (caption.length > 0) {
        locator = locator + "[@caption='" + caption + "']";
    }
    return locator;
};

function displayShell() {
    if (cwd == desktop || getClassName(cwd) == "Desktop")
    	print("/");
    else {
    	try {
    		print(getSimpleLocator(cwd) + "/");
    	} catch (e) {
    		printLine("  Current test object is no longer alive. Changing to desktop...");
    		cwd = desktop;
    		print("/");
    	}
    }
};

function run(script) {
	var allArgs = script.split(" ");
	var args = new Array(allArgs.length - 1);
	var jscript = ".\\bin\\" + allArgs[0] + ".js";
	var i = 0;
	for (i = 1; i < allArgs.length; ++i)
	args[i - 1] = allArgs[i];
	try {
		if (!fileSystem.fileExists(jscript))
			return false;
	var file = fileSystem.openTextFile(jscript, 1);
	var code = file.readAll();
	{
	    eval(code);
	}
	} catch (e) {
	printLine("  error: " + e.message);
	}
	return true;
}

printLine("Welcome to the Open Agent console. Type 'man' to get an introduction.");
while (true) {
    try {
        displayShell();
        var command = readLine();
        if (command.substring(0, 2) == "./") { // test object methods
        	var result = eval("cwd." + command.substring(2, command.length));
        	printLine(result);
        } else if (!run(command)) {
        	var result = eval(command);
        	if (command.indexOf("=") == -1)
        		printLine(result);
        }
    }
    catch (e) {
        if (e.message.length > 0) {
            printLine("  error: " + e.message);
        } else {
            printLine("  <unknown error>");
        }
    };
};
